package com.sdp.right.control;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.db.QueryColumn;
import com.sdp.core.bizc.threadLocal.ContextManager;
import com.sdp.core.bizc.utils.CoreConstantsEnum;
import com.sdp.core.bizc.utils.SnowFlake;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.cache.clean.RightCleanCache;
import com.sdp.right.db.sys_dict.service.ISysDictService;
import com.sdp.right.db.sys_dict_item.service.ISysDictItemService;
import com.sdp.right.entity.SysDict;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2023-10-11
 */
@RestController
@RequestMapping("/SysDictController")
public class SysDictController {
	
	@Autowired
	private ISysDictService iSysDictService;
	
	@Autowired
	private ISysDictItemService iSysDictItemService;
	
	@Autowired
	private SnowFlake snowFlake;
	
	@Autowired
	private Query query;
	
	@Autowired
	private RightCleanCache rightCleanCache;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		if(dataCenter.getQueryForm()==null || dataCenter.getQueryForm().size()==0) {
			return Result.error("数据检索条件不完整,必须包含模块号和字典类型");
		}
		String systemNo="";
		String dictType = "";
		List<QueryColumn> queryFrom = dataCenter.getQueryForm();
		for(QueryColumn qc : queryFrom) {
			if(CoreConstantsEnum.header中的模块参数名.getCode().equals(qc.getField())) {
				systemNo = qc.getVal();
			}else if("dictType".equals(qc.getField())) {
				dictType = qc.getVal();
			}
		}
		
		if(StringUtils.isEmpty(systemNo) || StringUtils.isEmpty(dictType)) {
			return Result.error("查询条件模块号和字典类型不存在，不能查询");
		}
		return Result.ok(query.page(iSysDictService, SysDict.class, dataCenter));
	}
	
	
	@PostMapping(value = "/save")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> save(@RequestBody SysDict sysDict) {
		if(sysDict==null) {
			return Result.error("数据字典信息为空");
		}
		if(StringUtils.isEmpty(sysDict.getDictOrder())) {
			sysDict.setDictOrder(0);
		}
		if(StringUtils.isEmpty(sysDict.getKeyId())) {
			sysDict.setDictCode(snowFlake.createOrderNo());
			if(StringUtils.isEmpty(sysDict.getSystemNo())) {
				String systemNo = ContextManager.getContextData(CoreConstantsEnum.header中的模块参数名.getCode());
				sysDict.setSystemNo(systemNo);
			}
			
			iSysDictService.save(sysDict);
		}else {
			iSysDictService.updateById(sysDict);
		}
		rightCleanCache.cleanSysDictCache();
		//删除缓存
		rightCleanCache.cleanSysDictItemCache(sysDict.getDictCode());
		rightCleanCache.cleanSysDictCache();
		return Result.ok();
		
	}
	
	@GetMapping(value = "/del")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> del(@RequestParam(name="keyId",required=true) String keyId,@RequestParam(name="dictCode",required=true) String dictCode) {
		if(StringUtils.isEmpty(keyId) || StringUtils.isEmpty(dictCode)) {
			return Result.error("传入参数不完整，不能删除");
		}
		iSysDictService.removeById(keyId);
		iSysDictItemService.removeSysDictItem(dictCode);
		//删除缓存
		rightCleanCache.cleanSysDictItemCache(dictCode);
		rightCleanCache.cleanSysDictCache();
		return Result.ok();
	}
	
	
	
	/**
	 * 根据字典编号获取字典信息
	 * @param dictCode
	 * @return
	 */
	@GetMapping(value = "/getDictInfo")
	public Result<?> getDictInfo(@RequestParam(name="dictCode",required=true) String dictCode,@RequestParam(name="dictParam",defaultValue="") String dictParam) {
		String arg [] = {};
		if(!StringUtils.isEmpty(dictParam) && dictParam.length()>0) {
			arg = dictParam.split(",");
		}
		JSONObject dictInfo = new JSONObject();
		if(!StringUtils.isEmpty(dictCode)) {
			String[] dictCodes = dictCode.split(",");
			for(int i=0;i<dictCodes.length;i++) {
				dictInfo.put(dictCodes[i], iSysDictService.getDictInfo(dictCodes[i],arg));
			}
		}
		return Result.ok(dictInfo);
		
	} 

}

